﻿using Microsoft.AspNetCore.Mvc;
using Model.DomainModel;
using Utility;
using Utility.Enum;
using ApprovalFlow_WebApi.PlugIn;
using DomainService.IService.Sys;

namespace ApprovalFlow_WebApi.Controllers
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.SysUser))]
    public class SysUserController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 日志服务
        /// </summary>

        public ISysLoginLogService _sysLoginLogService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysLoginLogService"></param>
        public SysUserController(ISysLoginLogService sysLoginLogService)
        {
            _sysLoginLogService = sysLoginLogService;
        }

        #endregion

        /// <summary>
        /// 获取登录日志,并写入数据库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ReceiveStatus<LoginLog>> GetLoginOrInsert()
        {
            ReceiveStatus<LoginLog>? receiveStatus = new();
            var model = GetLoginUserMsg();

            //地理位置信息
            var positionModel = await LoginPlugIn.GetLoginPositionMsg(HttpContext);

            LoginLog sysLoginLog = new LoginLog
            {
                Id = Guid.NewGuid().ToString(),
                CorporationKey = model.CorporationKey ?? string.Empty,
                UserId = model.UserId ?? string.Empty,
                Ip = positionModel?.ip,
                Country = positionModel?.data?.country,
                Prov = positionModel?.data?.prov,
                City = positionModel?.data?.city,
                Area = positionModel?.data?.area,
                Isp = positionModel?.data?.isp,
                Lng = positionModel?.data?.lng,
                Lat = positionModel?.data?.lat,
                CreateTime = DateTime.Now,
                CreateUser = model.UserId ?? string.Empty
            };
            _sysLoginLogService.Insert(sysLoginLog);
            receiveStatus.data = sysLoginLog;
            return receiveStatus;
        }

    }
}
